package com.penngo.gui.dfdiff.view;


import com.penngo.gui.dfdiff.DFMain;
import com.penngo.gui.dfdiff.utils.GB;
import com.penngo.gui.dfdiff.utils.ImgUtil;

import javax.swing.*;
import java.awt.*;
import java.util.List;


public class TabbedPaneClose extends JTabbedPane {

    public TabbedPaneClose() {
        super();

    }

    class TitleTab extends JPanel{
        private JLabel titleLabel;
        public TitleTab(String title, String toolTipText) {
            super(new GridBagLayout());
            setOpaque(false);
            titleLabel = new JLabel(title);
            JButton closeButton = new JButton();

            // 设置关闭按钮的图标
            closeButton.setIcon(ImgUtil.getIcon("/images/close-line-16.png"));
            closeButton.setRolloverIcon(ImgUtil.getIcon("/images/close-circle-line16.png"));
            closeButton.setPreferredSize(new Dimension(15, 15));
            closeButton.setToolTipText("关闭");
            closeButton.setBorderPainted(false); // 去掉按钮边框
            closeButton.setContentAreaFilled(false); // 去掉按钮背景填充

            // 添加关闭按钮的动作监听器
            closeButton.addActionListener(e -> {
                int indexTab = indexOfTabComponent(this);
                if (indexTab != -1) {
                    if (TabbedPaneClose.this.getComponentAt(indexTab) instanceof RightPanel rightPanel){
                        ProjectNode node = rightPanel.getProjectNode();
                        ProjectNode projectNode = (ProjectNode) DFMain.projectTable.getRootNode();
                        List<ProjectNode> children = projectNode.getChildren();
                        for(ProjectNode tempNode:children){
                            if(tempNode.equals(node)){
                                children.remove(node);
                                node.setPanel(null);
                                removeTabAt(indexTab);
                                break;
                            }
                            else if(!tempNode.getChildren().isEmpty()){
                                removeNode(tempNode.getChildren(), node, indexTab);
                            }
                        }
                    }
                }
            });

            add(titleLabel, new GB(0,0,1,1).setFill(GB.BOTH).setWeight(100,100));
            add(closeButton, new GB(1,0,1,1).setInsets(0,5,0,0));
        }
        public void setTitle(String text){
            titleLabel.setText(text);
        }
    }

    public void addTab(String title, String toolTipText, Component component) {
        TitleTab tabPanel = new TitleTab(title, toolTipText);
//        JPanel tabPanel = new JPanel(new GridBagLayout());
//        tabPanel.setOpaque(false);

//        JLabel label = new JLabel(title);
//        JButton closeButton = new JButton();
//
//        // 设置关闭按钮的图标
//        closeButton.setIcon(ImgUtil.getIcon("/images/close-line-16.png"));
//        closeButton.setRolloverIcon(ImgUtil.getIcon("/images/close-circle-line16.png"));
//        closeButton.setPreferredSize(new Dimension(15, 15));
//        closeButton.setToolTipText("关闭");
//        closeButton.setBorderPainted(false); // 去掉按钮边框
//        closeButton.setContentAreaFilled(false); // 去掉按钮背景填充
//
//        // 添加关闭按钮的动作监听器
//        closeButton.addActionListener(e -> {
//            int indexTab = indexOfTabComponent(tabPanel);
//            if (indexTab != -1) {
//                if (getComponentAt(indexTab) instanceof RightPanel rightPanel){
//                    ProjectNode node = rightPanel.getProjectNode();
//                    ProjectNode projectNode = (ProjectNode) DFMain.projectTable.getRootNode();
//                    List<ProjectNode> children = projectNode.getChildren();
//                    for(ProjectNode tempNode:children){
//                        if(tempNode.equals(node)){
//                            children.remove(node);
//                            node.setPanel(null);
//                            removeTabAt(indexTab);
//                            break;
//                        }
//                        else if(!tempNode.getChildren().isEmpty()){
//                            removeNode(tempNode.getChildren(), node, indexTab);
//                        }
//                    }
//                }
//            }
//        });
//
//        tabPanel.add(label, new GB(0,0,1,1).setFill(GB.BOTH).setWeight(100,100));
//        tabPanel.add(closeButton, new GB(1,0,1,1).setInsets(0,5,0,0));

        // 使用自定义的tabPanel作为选项卡标题
        super.addTab(null, component);
        setTabComponentAt(getTabCount() - 1, tabPanel);
    }

    public void setTitle(String title, Component component){
        int indexTab = indexOfTabComponent(component);
        Component com = getTabComponentAt(indexTab);
        if (com instanceof TitleTab titleTab){
            titleTab.setTitle(title);
        }
    }

    public boolean removeNode(List<ProjectNode> children, ProjectNode node, int indexTab) {
        boolean isRemove = false;
        for (ProjectNode tempNode:children){
            if(tempNode.equals(node)){
                children.remove(tempNode);
                node.setPanel(null);
                removeTabAt(indexTab);
                isRemove = true;
                break;
            }
            else if (!tempNode.getChildren().isEmpty()){
                removeNode(tempNode.getChildren(), node, indexTab);
            }
        }
        return isRemove;
    }

}

